package com.duowan.realtime.server.util;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author huzhirong
 * 
 */
public class SpringContextFactory {

	private static String[] filenames = {
		"classpath:spring-data-computing-server/applicationContext*.xml"
	};

	private static AbstractApplicationContext applicationContext = null;

	public static synchronized AbstractApplicationContext getApplicationContext() {
		if(applicationContext == null) {
			applicationContext = new ClassPathXmlApplicationContext(filenames);
		}
		return applicationContext;
	}

	public static void autowireBeanPropertiesByName(Object existingBean) {
		SpringContextFactory.getApplicationContext().getAutowireCapableBeanFactory().autowireBeanProperties(existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
	}

	public static Object getBean(String beanName) {
		return applicationContext.getBean(beanName);
	}

	public static void shutDown() {
		applicationContext.registerShutdownHook();
	}
	
	public static void setFilenames(String[] applicationContextFilenames){
		filenames = applicationContextFilenames;
	}

	public static String[] getFilenames(){
		return filenames;
	}
	public static void main(String[] args) {

	}
}
